home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OpenTptCommon.h
-
- Contains: Equates for Open Transport development needed both by clients
- and by kernel.
-
- Copyright: © 1993-1996 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
-
-
- */
-
- #ifndef __OPENTPTCOMMON__
- #define __OPENTPTCOMMON__
-
- #ifndef __OPENTRANSPORT__
- #include <OpenTransport.h>
- #endif
- #ifndef _MPS_STREAM_
- #include <mistream.h>
- #endif
-
- #if GENERATING68K && defined(__MWERKS__)
- #pragma pointers_in_D0
- #endif
-
- /*******************************************************************************
- ** Some typedefs
- ********************************************************************************/
-
- typedef long OTCommand; /* The command code in STREAMS messages */
- typedef SInt16 OTRelease; /* An internal OT typedef */
-
- /*******************************************************************************
- ** Some private IOCTL constants we need to know
- ********************************************************************************/
-
- enum
- {
- I_OTYieldPort = MIOC_CMD(MIOC_OT, 4) /* request to yield the port */
- };
-
- /*******************************************************************************
- ** Some defines
- ********************************************************************************/
-
- #define MI_BIG_ENDIAN 1
- #undef MI_LITTLE_ENDIAN
-
- //
- // For static members that need to match pascal functions
- //
- #define _FSDECL static pascal
- #define _FSDEF pascal
-
- /* -------------------------------------------------------------------------
- ** Bitmap functions
- **
- ** These functions deal with a bitmap that is multiple-bytes long
- ------------------------------------------------------------------------- */
-
- #ifdef __cplusplus
- extern "C" {
- #endif
- //
- // Set the first clear bit in "bitMap", starting with bit "startBit",
- // giving up after "numBits". Returns the bit # that was set, or
- // a kOTNotFoundErr if there was no clear bit available
- //
- OTResult OTSetFirstClearBit(UInt8* bitMap, size_t startBit, size_t numBits);
- //
- // Standard clear, set and test bit functions
- //
- Boolean OTClearBit(UInt8* bitMap, size_t bitNo);
- Boolean OTSetBit(UInt8* bitMap, size_t bitNo);
- Boolean OTTestBit(UInt8* bitMap, size_t bitNo);
-
- #ifdef __cplusplus
- }
- #endif
-
- /* -------------------------------------------------------------------------
- ** OTHashList
- **
- ** This implements a simple, but efficient hash list. It is not
- ** thread-safe.
- ------------------------------------------------------------------------- */
-
- typedef struct OTHashList OTHashList;
-
- typedef UInt32 (* _CDECL OTHashProcPtr)(OTLink* linkToHash);
- typedef Boolean (* _CDECL OTHashSearchProcPtr)(const void* ref, OTLink* linkToCheck);
-
- #ifdef __cplusplus
- extern "C" {
- #endif
- //
- // Return the number of bytes of memory needed to create a hash list
- // of at least "numEntries" entries.
- //
- size_t OTCalculateHashListMemoryNeeds(size_t numEntries);
- //
- // Create an OTHashList from "memory". Return an error if it
- // couldn't be done.
- //
- OTResult OTInitHashList(void* memory, size_t numBytes, OTHashProcPtr);
- void OTAddToHashList(OTHashList*, OTLink*);
- Boolean OTRemoveLinkFromHashList(OTHashList*, OTLink*);
- Boolean OTIsInHashList(OTHashList*, OTLink*);
- OTLink* OTFindInHashList(OTHashList*, OTHashSearchProcPtr proc,
- const void* refPtr, UInt32 hashValue);
- OTLink* OTRemoveFromHashList(OTHashList*, OTHashSearchProcPtr proc,
- const void* refPtr, UInt32 hashValue);
-
- #ifdef __cplusplus
- }
- #endif
-
- struct OTHashList
- {
- OTHashProcPtr fHashProc;
- size_t fHashTableSize;
- OTLink** fHashBuckets;
-
- #ifdef __cplusplus
- void Add(OTLink* toAdd)
- { OTAddToHashList(this, toAdd); }
-
- Boolean RemoveLink(OTLink* toRemove)
- { return OTRemoveLinkFromHashList(this, toRemove); }
-
- OTLink* Remove(OTHashSearchProcPtr proc,
- const void* refPtr, UInt32 hashValue)
- { return OTRemoveFromHashList(this, proc, refPtr, hashValue); }
-
- Boolean IsInList(OTLink* toFind)
- { return OTIsInHashList(this, toFind); }
-
- OTLink* FindLink(OTHashSearchProcPtr proc, const void* refPtr,
- UInt32 hash)
- { return OTFindInHashList(this, proc, refPtr, hash); }
- #endif
- };
-
-
- /* -------------------------------------------------------------------------
- ** Random functions
- **
- ** These implement a very simple random number generator
- ------------------------------------------------------------------------- */
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- UInt32 OTGetRandomSeed();
- UInt32 OTGetRandomNumber(UInt32* seed, UInt32 lo, UInt32 hi);
-
- #ifdef __cplusplus
- }
- #endif
-
-
- /*******************************************************************************
- ** XmitRecord Class
- **
- ** This class is used when sending "write"-type commands, where we need
- ** information to be able to complete a write to the client when it is
- ** finished.
- ********************************************************************************/
-
- struct XmitRecord
- {
- OTLink fLink;
- void* provider;
- void* dataBuf;
- size_t dataLen;
- SInt16 ackCount;
- };
-
- /*******************************************************************************
- ** Miscellaneous functions
- ********************************************************************************/
-
- #ifdef __cplusplus
- extern "C" {
- #endif
- //
- // Return true if we are at interrupt level
- //
- Boolean OTIsAtInterruptLevel(void);
- //
- // Return true if we are currently at either system task time, or we
- // are running a deferred task procedure from system task time.
- //
- Boolean OTCanLoadLibraries(void);
- void OTEnterCriticalSection(void);
- void OTLeaveCriticalSection(void);
-
- #ifdef __cplusplus
- }
- #endif
-
- /*******************************************************************************
- ** Port ref functions
- ********************************************************************************/
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- pascal OTPortRef OTSetDeviceTypeInPortRef(OTPortRef ref, UInt16 devType);
- pascal OTPortRef OTSetBusTypeInPortRef(OTPortRef ref, UInt8 busType);
-
- pascal Boolean OTMakePortName(char* buffer, const char* moduleName,
- OTPortRef);
-
- #ifdef __cplusplus
- }
- #endif
-
- /*******************************************************************************
- ** Some defines
- ********************************************************************************/
-
- /* -------------------------------------------------------------------------
- IOCtls used by OpenTransport
- ------------------------------------------------------------------------- */
-
- typedef int OTNotifyType;
-
- enum
- {
- kOTNotifyAllModules = 0, kOTNotifyInterestedModules = 1,
- kOTNotifyControlModules = 2
- };
-
- struct OTIOCtlNotifyInfo
- {
- OTEventCode fCode; // Event Code
- void* fCookie; // Cookie associated with it
- UInt32 fNotifyType;// Who to notify
- };
-
- typedef struct OTIoctlNotifyInfo OTIoctlNotifyInfo;
-
- enum
- {
- I_OTNotifyAllClients = MIOC_CMD(MIOC_OT, 50),
- I_OTSetPowerLevel = MIOC_CMD(MIOC_OT, 51)
- };
-
- /* -------------------------------------------------------------------------
- Some equates for kPROTOCOLEVENTs that normal clients don't need to
- know.
- ------------------------------------------------------------------------- */
-
- enum
- {
- kHiPriProtocolEvent = 0x08000000 /* Bit to be a high-priority event */
- };
-
- #define IsHiPriProtocolEvent(x) (((x) & 0xff000000) == (kPROTOCOLEVENT | kHiPriProtocolEvent))
- #define StripProtocolEvent(x) ((x) & ~kHiPriProtocolEvent)
- //
- // Use this template to define your preferences if you use
- // the TStreamGroup and TStreamFamily infrastructure
- //
- #define OTPreferencesFields(num) \
- UInt16 fVersion; \
- UInt16 fNumPrefs; \
- OTPortRef fPort; \
- OTLink fLink; \
- void* fPrefs[num]
- //
- // This template is used to define the first set
- // of fields in each individual preference array
- //
- #define OTPreferenceFields \
- UInt16 fVersion; \
- UInt16 fSize
-
- //
- // Define to tell infrastructure you want the main preference
- // structure instead of a substructure.
- //
- enum
- {
- kOTPrefStructureCode = (UInt32)0xffffffffU
- };
-
-
- /*******************************************************************************
- ** Functions to convert xti/mac errors to OSStatus'
- ********************************************************************************/
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- pascal OSStatus OTConvertError(OTXTIErr xtiErr, OTUnixErr macErr);
-
- #ifdef __cplusplus
- }
- #endif
-
- #ifdef __cplusplus
- //
- // Macro for Speed
- //
- inline OSStatus OTDoConvertError(long xtiErr, long macErr)
- {
- return xtiErr == 0 ? kOTNoError : OTConvertError((OTXTIErr)xtiErr, (OTUnixErr)macErr);
- }
-
- #else
-
- #define OTDoConvertError(xtiErr, macErr) \
- ((xtiErr) == 0 ? kOTNoError : OTConvertError((OTXTIErr)(xtiErr), (OTUnixErr)(macErr)))
-
- #endif
-
- /*******************************************************************************
- ** Some efficient implementations of BigEndian and LittleEndian words
- ********************************************************************************/
-
- #ifdef MI_BIG_ENDIAN
- //
- // Since Open Transport is 68020 and better only, we don't have to
- // worry about alignment problems.
- //
- #define GetBigEndian16At(ptr) (*(UInt16*)(ptr))
- #define GetBigEndian32At(ptr) (*(UInt32*)(ptr))
- #define SetBigEndian16At(ptr, val) { *(UInt16*)(ptr) = (UInt16)(val); }
- #define SetBigEndian32At(ptr, val) { *(UInt32*)(ptr) = (UInt32)(val); }
-
- #else
-
- #define GetBigEndian16At(ptr) \
- ((Uint16)(((((UInt8*)ptr)[0]) << 8) + ((UInt8*)ptr)[1]))
-
- #define GetBigEndian32At(ptr) \
- ((UInt32)(((((UInt8*)ptr)[0]) << 24) + \
- (((UInt8*)ptr)[1] << 16) + \
- (((UInt8*)ptr)[2] << 8) + \
- ((UInt8*)ptr)[3]))
-
- #define SetBigEndian16At(ptr, val) \
- { \
- ((UInt8*)ptr)[0] = (UInt8)(val >> 8); \
- ((UInt8*)ptr)[1] = (UInt8)(val); \
- }
-
- #define SetBigEndian32At(ptr, val) \
- { \
- ((UInt8*)ptr)[0] = (UInt8)(val >> 24); \
- ((UInt8*)ptr)[1] = (UInt8)(val >> 16); \
- ((UInt8*)ptr)[2] = (UInt8)(val >> 8); \
- ((UInt8*)ptr)[3] = (UInt8)(val); \
- }
-
- #endif
-
- #ifdef MI_LITTLE_ENDIAN
-
- #define GetLittleEndian16At(ptr) (*(UInt16*)(ptr))
- #define GetLittleEndian32At(ptr) (*(UInt32*)(ptr))
- #define SetLittleEndian16At(ptr, val) { *(UInt16*)(ptr) = (UInt16)(val); }
- #define SetLittleEndian32At(ptr, val) { *(UInt32*)(ptr) = (UInt32)(val); }
-
- #else
-
- #define GetLittleEndian16At(ptr) \
- ((Uint16)(((((UInt8*)ptr)[1]) << 8) + ((UInt8*)ptr)[0]))
-
- #define GetLittleEndian32At(ptr) \
- ((UInt32)(((((UInt8*)ptr)[3]) << 24) + \
- (((UInt8*)ptr)[2] << 16) + \
- (((UInt8*)ptr)[1] << 8) + \
- ((UInt8*)ptr)[0]))
-
- #define SetLittleEndian16At(ptr, val) \
- { \
- ((UInt8*)ptr)[1] = (UInt8)(val >> 8); \
- ((UInt8*)ptr)[0] = (UInt8)(val); \
- }
-
- #define SetLittleEndian32At(ptr, val) \
- { \
- ((UInt8*)ptr)[3] = (UInt8)(val >> 24); \
- ((UInt8*)ptr)[2] = (UInt8)(val >> 16); \
- ((UInt8*)ptr)[1] = (UInt8)(val >> 8); \
- ((UInt8*)ptr)[0] = (UInt8)(val); \
- }
-
- #endif
-
- #ifdef __cplusplus
-
- /* -------------------------------------------------------------------------
- TOTBaseObject
-
- This is the base class for Open Transport objects that DO NOT need to
- be 68K aligned.
- ------------------------------------------------------------------------- */
-
- #if USESINGLEOBJECT
- class TOTBaseObject : public SingleObject
- #else
- class TOTBaseObject
- #endif
- {
- EXTRA_VTABLE_SLOT
-
- public:
- _CT TOTBaseObject() {}
- virtual _DT TOTBaseObject();
-
- private:
- _CT TOTBaseObject(const TOTBaseObject&);
- void operator=(const TOTBaseObject&);
- };
-
- #if GENERATINGPOWERPC
- #pragma options align=mac68k
- #endif
-
- /* -------------------------------------------------------------------------
- TOTAlignedObject
-
- This is the base class for Open Transport objects that DO need to
- be 68K aligned.
- ------------------------------------------------------------------------- */
-
- #if USESINGLEOBJECT
- class TOTAlignedObject : public SingleObject
- #else
- class TOTAlignedObject
- #endif
- {
- EXTRA_VTABLE_SLOT
-
- public:
- _CT TOTAlignedObject() {}
- virtual _DT TOTAlignedObject();
-
- private:
- _CT TOTAlignedObject(const TOTAlignedObject&);
- void operator=(const TOTAlignedObject&);
- };
-
- #if GENERATINGPOWERPC
- #pragma options align=reset
- #endif
-
- #endif
-
- #if GENERATING68K && defined(__MWERKS__)
- #pragma pointers_in_A0
- #endif
-
- #endif /* __OPENTPTCOMMON__ */
-